#include "aidialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QBitmap>
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QDebug>


/**
 * @brief AiDialog::AiDialog
 * @param parent
 * AI麒麟百事通,调用阿里云的开源通义千问接口
 */


AiDialog::AiDialog(QWidget* parent) : QDialog(parent) {
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    recorder =  new QAudioRecorder;
    if(recorder->defaultAudioInput().isEmpty())
    {
        return; //无音频录入设备
    }
    foreach(QString a, recorder->supportedContainers())
    {
        qDebug() << a;
    }
    // 设置透明度
    setWindowOpacity(0.9);
    setFixedSize(580, 380);
    //设置圆角
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),20,20);//像素为10的圆角
    setMask(bmp);
    setupUI();
}

void AiDialog::mouseDoubleClickEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        this->close();
    }
}

//手动实现界面随着鼠标的拖动而移动
void AiDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        lastMousePosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}
void AiDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - lastMousePosition);
        event->accept();
    }
}

bool AiDialog::eventFilter(QObject *obj, QEvent *event)
{
    //鼠标进入
    if ((obj->inherits("QLabel")) && event->type() == QEvent::MouseButtonPress)
    {
        //qDebug() << "hh";
        QPixmap img(":/icons/images/ai.png");
        int width = 100;
        int height = 100;
        img = img.scaled(width, height, Qt::KeepAspectRatio);
        label->setPixmap(img);
        emit labelClicked();
    }
    //鼠标离开
    else if ((obj->inherits("QLabel")) && event->type() == QEvent::MouseButtonRelease)
    {
        //qDebug() << "aa";
        QPixmap img(":/icons/images/ai.png");
        int width = 130;
        int height = 130;
        img = img.scaled(width, height, Qt::KeepAspectRatio);
        label->setPixmap(img);
        emit labelReleased();
    }
    else if ((obj->inherits("QLabel")) && event->type() == QEvent::Enter)
    {
        emit labelHover();
    }
    return QDialog::eventFilter(obj, event);
}

// 显示问题
void AiDialog::showQuestion(QString text)
{
    //labelQ->setVisible(true);
    labelQ->setText(text);
}

// 获得回答的答案
QString AiDialog::getText()
{
    return labelA->toPlainText();
}

// 显示AI大模型回答的答复
void AiDialog::showAnswer(QString text)
{
    labelA->setVisible(true);
    labelA->setText(text);
}

void AiDialog::setupUI() {
    // 创建渐变背景
    QLinearGradient gradient(0, 0, 0, height());
    gradient.setColorAt(0, QColor(255, 105, 180));  // 起始颜色
    gradient.setColorAt(1, QColor(65, 105, 225));  // 终止颜色

    QPalette palette;
    palette.setBrush(QPalette::Background, QBrush(gradient));
    setPalette(palette);

    // 创建内容布局
    QVBoxLayout* layout = new QVBoxLayout(this);

    label = new QLabel();
    label->installEventFilter(this);
    labelQ = new QLabel();
    labelA = new QTextBrowser();
    labelA->setStyleSheet(
            "QTextBrowser {"
            "    height: 80px; /* 宽度 */"
            "    width: 580px; /* 高度 */"
            "    background-color: #7B68EE; /* 背景颜色 */"
            "    border: 2px solid darkblue; /* 边框 */"
            "    border-radius: 15px; /* 圆角 */"
            "    padding: 10px; /* 内边距 */"
            "    color: white; /* 文本颜色 */"
            "    font-size: 22px; /* 字体大小 */"
            "}"
        );
    QFont font("Arial", 18);
    label->setStyleSheet("color: white;");
    labelQ->setFont(font);
    labelA->setFont(font);
    QPixmap img(":/icons/images/ai.png");
    int width = 130;
    int height = 130;
    img = img.scaled(width, height, Qt::KeepAspectRatio);
    label->setPixmap(img);

    // 创建旋转动画
    //QPropertyAnimation animation(label, "rotation");
    //animation.setDuration(5000); // 旋转动画的持续时间（毫秒）
    //animation.setStartValue(0);
    //animation.setEndValue(360);
    //animation.setLoopCount(-1); // 无限循环

    // 启动动画
    //animation.start();

    layout->addWidget(label, 0, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(labelQ, 0, Qt::AlignBottom | Qt::AlignHCenter);
    layout->addWidget(labelA, 0, Qt::AlignHCenter | Qt::AlignHCenter);

    // 设置布局
    setLayout(layout);
    setWindowOpacity(0.8);
    labelA->setVisible(false);
}

AiDialog::~AiDialog() {
    delete label;
    delete labelQ;
    delete labelA;
    delete recorder;
}
